package com.clhbolg.listener;

import cn.hutool.core.convert.Convert;
import com.clhbolg.domain.Article;
import com.clhbolg.enums.OperationEnum;
import com.clhbolg.event.ArticleEvent;
import com.clhbolg.mapper.es.ArticlesSearchMapper;
import com.clhbolg.model.es.ArticleSearchDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;

/**
 * @Author cailinhu
 * @Description 文章监听器
 * @Date 2023/3/14 15:16
 * @Version 1.0
 */
//@Component
public class ArticleListener {

    @Autowired
    ArticlesSearchMapper articlesSearchMapper;

    @EventListener
    public void article(ArticleEvent articleEvent){
        Article article= (Article)articleEvent.getSource();
        ArticleSearchDTO convert = Convert.convert(ArticleSearchDTO.class, article);
        OperationEnum operationEnum = articleEvent.getOperationEnum();
        switch (operationEnum){
            case INSERT:
                articlesSearchMapper.insert(convert);
                break;
            case UPDATE:
                articlesSearchMapper.updateById(convert);
                break;
            case DEL:
                articlesSearchMapper.deleteById(convert.getId());
        }

    }

}
